package com.portal4kids.controller;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.portal4kids.model.Holidays;
import com.portal4kids.model.Party;
import com.portal4kids.util.HibernateUtil;

public class PartyManager extends HibernateUtil{
	
	public Party saveParty(Party party){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		party.setAvarage(0);
		party.setNumberOfVotes(0);
		party.setRating(0);
		party.setComments("");
		session.save(party);
		session.getTransaction().commit();
		return party;
	}
	
	public List<Party> getPartyFromDB(Party party){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Party> partyList = new ArrayList<Party>();
		if("Dowolna".equals(party.getAddress())){
			if("TAK".equals(party.getCatering()) && "TAK".equals(party.getAnimator())){
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and catering='" + party.getCatering() + "' and animator='"+ party.getAnimator() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}else if("TAK".equals(party.getCatering()) && "NIE".equals(party.getAnimator())){
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and catering='" + party.getCatering() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}else if("NIE".equals(party.getCatering()) && "TAK".equals(party.getAnimator())){
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and animator='"+ party.getAnimator() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}else{
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}
		}else{
			if("TAK".equals(party.getCatering()) && "TAK".equals(party.getAnimator())){
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and address='"+ party.getAddress() + "' and catering='" + party.getCatering() + "' and animator='"+ party.getAnimator() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}else if("TAK".equals(party.getCatering()) && "NIE".equals(party.getAnimator())){
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and address='"+ party.getAddress() + "' and catering='" + party.getCatering() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}else if("NIE".equals(party.getCatering()) && "TAK".equals(party.getAnimator())){
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and address='"+ party.getAddress() + "' and animator='"+ party.getAnimator() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}else{
				Query q = session.createQuery("from Party where type='"+ party.getType() + "' and address='"+ party.getAddress() + "' ORDER BY(avarage) DESC");
				partyList = q.list();
			}
		}
		
		session.getTransaction().commit();
		return partyList;
	}
	
	public List<Party> getPartyAfterID(int id){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		List<Party> partyList = new ArrayList<Party>();
		Query q = session.createQuery("from Party where id_party='"+ id + "'");
		partyList = q.list();
		session.getTransaction().commit();
		return partyList;
	}
	
	public static void addRank(int id_place, int rank){
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();
		Party party = (Party) session.load(Party.class, id_place);
		
//		DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance();
//		symbols.setDecimalSeparator('.');
//		DecimalFormat df = new DecimalFormat("#.##", symbols);
		
		int numberOfVotes = party.getNumberOfVotes();
		numberOfVotes++;
		party.setNumberOfVotes(numberOfVotes);
		
		int currentRank = party.getRating();
		currentRank += rank;
		party.setRating(currentRank);
		if(numberOfVotes == 0){
			System.out.println("0000000000000000000000");
		}else{
		int avarage = currentRank / numberOfVotes;
		party.setAvarage(avarage);
		}
		session.update(party);
		session.getTransaction().commit();

	}
}
